Repository navigation
perf: encode only the path in ItemWithPath instead of building request information - #3234
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The trailing-colon trimming and last-segment behavior lack regression coverage for colon-containing paths through both ItemWithPath overloads.
1 open issue
What changed in this PR
This PR reduces construction overhead for path-based drive-item requests by encoding the path directly, without changing public API signatures or generated code.
Changes:
- Replaces full request-URI construction in both
ItemWithPathoverloads with a shared privateEncodePathhelper. - Retains the last non-empty colon-separated segment rule.
- Defers failures from unresolvable builder URIs until request creation or sending.
| File | Description |
|---|---|
src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.cs |
Adds path-only encoding and uses it in both ItemWithPath overloads. |
Review effort: Balanced
| while (end > 0 && encoded[end - 1] == ':') | ||
| end--; | ||
| var start = end == 0 ? 0 : encoded.LastIndexOf(':', end - 1) + 1; |
There was a problem hiding this comment.
Added in 0e6c3cb: two theories, one per ItemWithPath overload, covering a:b, a:b::, /::, folder:name/file.txt and item/with/path:. Each asserts the request URL and the Children builder URL. The expected values were taken from main, so they pin the previous behaviour.
Note that for a path such as a:b only b is kept. That is how main behaves today and this PR does not change it; I can open a separate issue if it is not intended.

Changes proposed in this pull request
ItemWithPath(both overloads) calledToGetRequestInformation(), expanded the URL template into a full request URI, appended the path throughUriBuilder, built aUriand split the whole URL on:. All of that was only used to URL-encode thepathargument; the request information itself was thrown away.EncodePathhelper. It uses the same encoder as before (UriBuilderescapes the path when it is assigned) and keeps the same "last non-empty:separated part" rule, so the produced URL template is unchanged.src/Microsoft.Graph/Extensions/DriveItemRequestBuilderExtensions.csis touched. No generated code and no public API change.One behaviour note:
ItemWithPathno longer builds the request URI, so a builder whose URI cannot be resolved would now fail when the request is created or sent instead of insideItemWithPath.Same output
A console check ran both overloads over 299,143 random paths (spaces,
%,#,?,:,\, quotes, brackets, non-ASCII, tabs) againstmainand against this branch and hashed the resultingUrlTemplateandURIof a child builder for every path. Both builds give the same SHA-256, and neither throws for any of the paths. This was run on .NET 10 only.Benchmark
BenchmarkDotNet 0.15.2,
[MemoryDiagnoser], .NET 10.0.14, x64, Windows 11, Release build ofMicrosoft.Graph(net10.0),mainvs this branch._root.ItemWithPath("a/b/c")_item.ItemWithPath("a/b/c")_client.Drives["d"].Root.ItemWithPath("a/b/c").ContentThese are builder-construction costs in nanoseconds; next to the HTTP call that follows they are small in absolute terms. The gain matters mostly for code that builds many path-based requests, for example when filling batches.
Testing
dotnet build Microsoft.Graph.sln -c Release: succeeded, 0 errors, all target frameworks (netstandard2.0, netstandard2.1, net8.0, net10.0).dotnet test tests/Microsoft.Graph.DotnetCore.Test -c Release --filter "FullyQualifiedName!~Functional": 106 passed, 0 failed, 0 skipped on net8.0 and on net10.0 (96 existing tests plus 10 new cases). The Functional tests were not run because they need a live tenant.DriveItemRequestBuilderExtensionsTests, one perItemWithPathoverload, for paths that contain colons (a:b,a:b::,/::,folder:name/file.txt,item/with/path:). Each asserts the request URL and theChildrenbuilder URL. The expected values were taken frommain. No existing test was changed.Other links